home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / mahe / mahe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  6.6 KB  |  260 lines

  1. /* mahe.c        Max Rible
  2.  * Program that wraps around some useful adaptive histogram
  3.  * routines and makes them useful for us at the graphics lab.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <hipl_format.h>
  9. #include "ahe.h"
  10. /*#define DEBUG*/
  11.  
  12. #define Calloc(a,b) (b *) calloc((unsigned)(a), sizeof(b))
  13. #define Fread(a,b,c,d) fread((char *)(a), b, (int)(c), d)
  14. #define Fwrite(a,b,c,d) fwrite((char *)(a), b, (int)(c), d)
  15. #define Cfree(x,y,z) cfree((char *)(x), (unsigned)(y), z)
  16.  
  17. float clip;
  18. static int dim[3], nreg[3], minmax[2], imfmt;
  19. static struct header hipshead;
  20.  
  21. main(argc, argv)
  22.      int argc;
  23.      char *argv[];
  24. {
  25.     FILE *input, *output;
  26.     int i, j, imsize, err;
  27.     unsigned int *intbuf ;
  28.     unsigned char *inptr ;
  29.     unsigned short *in_short_ptr ;
  30.     unsigned char *in_char_image ;
  31.     unsigned short *in_short_image ;
  32.     unsigned char *outimage ;
  33.     float scale_io ;
  34.  
  35.     int min_int = 2000000000 ;
  36.     int max_int = 0 ;
  37.     unsigned char min_char = 255 ;
  38.     unsigned char max_char = 0 ;
  39.     unsigned short min_short = 65535 ;
  40.     unsigned short max_short = 0 ;
  41.     int tmpint ;
  42.     int num_frames ;
  43.     int frame_index ;
  44.     char *inname,*outname;
  45.  
  46.     Progname = strsave(*argv);
  47.     input = stdin;
  48.     output = stdout;
  49.     inname = "<stdin>";
  50.     outname = "<stdout>";
  51.     dim[0] = 1; dim[1] = dim[2] = 512;
  52.     nreg[0] = 1; nreg[1] = nreg[2] = 4;
  53.     clip = 0.0;
  54.  
  55.     parse_args(argc, argv);
  56.  
  57.     fread_header(input, &hipshead,inname);
  58.     imfmt = hipshead.pixel_format;
  59.     dim[2] = hipshead.ocols;
  60.     dim[1] = hipshead.orows;
  61.     num_frames = hipshead.num_frame ;
  62.     hipshead.pixel_format = PFBYTE;
  63.     update_header(&hipshead, argc, argv);
  64.  
  65.     imsize = dim[2]*dim[1];
  66.  
  67.     if((outimage = Calloc(imsize, unsigned char)) == NULL)
  68.     perror("outimage");
  69.     fprintf (stderr, "mahe begun\n");
  70.  
  71. for (frame_index = 0 ; frame_index < num_frames ; ++frame_index)
  72. {
  73.     switch(imfmt) {
  74.     case PFINT:
  75.         if((in_short_image = Calloc(imsize, unsigned short)) == NULL)
  76.         perror("in_short_image");
  77.  
  78.     if((intbuf = Calloc(dim[2], unsigned int)) == NULL)
  79.         perror("int buffer");
  80.     for(j = 0; j < dim[1]; j++)
  81.     {
  82.         Fread(intbuf, sizeof(int), dim[2], input);
  83.         for(i = 0; i < dim[2]; i++)
  84.         {
  85.         if (intbuf[i] > max_int)
  86.             max_int = intbuf[i] ;
  87.         if (intbuf[i] < min_int)
  88.             min_int = intbuf[i] ;
  89.         }
  90.     }
  91.         scale_io = ((float) MAXSHORT/((float) (max_int - min_int)));
  92. #ifdef DEBUG
  93.     fprintf (stderr,"%d %d %f\n",min_int,max_int,scale_io) ;
  94. #endif
  95.     /* CHANGE! */
  96.     for(j = 0; j < dim[1]; j++)
  97.     {
  98.         Fread(intbuf, sizeof(int), dim[2], input);
  99.         for(i = 0; i < dim[2]; i++)
  100.                 in_short_image[j*dim[2] + i] = (unsigned short)((float)(in_short_image[j*dim[2] + i] - min_int)) * scale_io + 0.5;
  101.     }
  102.     Cfree(intbuf, dim[2], sizeof(unsigned char));
  103.     break;
  104.     case PFBYTE:
  105.         if((in_char_image = Calloc(imsize, unsigned char)) == NULL)
  106.         perror("in_char_image");
  107.  
  108.     if(Fread(in_char_image, sizeof(char), imsize, input) != imsize)
  109.         perror("read");
  110.     inptr = in_char_image ;
  111.     for (i = 0 ; i < imsize ; i++)
  112.     {
  113.         if (*inptr > max_char)
  114.            max_char = *inptr ;
  115.         if (*inptr < min_char)
  116.            min_char = *inptr ;
  117.         ++inptr ;
  118.     }
  119.         scale_io = ((float) MAXCHAR/((float) (max_char - min_char)));
  120.     inptr = in_char_image ;
  121.     for(i = 0; i < imsize ; i++)
  122.     {
  123.                 tmpint = (int) (((float)(*inptr - min_char)) * scale_io + 0.5);
  124.         *inptr++ = (unsigned char) tmpint ;
  125.     }
  126.         minmax[0] = 0 ;
  127.         minmax[1] = 255 ;
  128.     break;
  129.     case PFSHORT:
  130.         if((in_short_image = Calloc(imsize, unsigned short)) == NULL)
  131.         perror("in_short_image");
  132.  
  133.     if(Fread(in_short_image, sizeof(short), imsize, input) != imsize)
  134.         perror("read");
  135.     in_short_ptr = in_short_image ;
  136.     for (i = 0 ; i < imsize ; i++)
  137.     {
  138.         if (*in_short_ptr > max_short)
  139.            max_short = *in_short_ptr ;
  140.         if (*in_short_ptr < min_short)
  141.            min_short = *in_short_ptr ;
  142.         ++in_short_ptr ;
  143.     }
  144.         scale_io = ((float) MAXSHORT/((float) (max_short - min_short)));
  145. #ifdef DEBUG
  146.     fprintf (stderr,"%d %d %f\n",min_short,max_short,scale_io) ;
  147. #endif
  148.     in_short_ptr = (unsigned short *) in_short_image ;
  149.     for(i = 0; i < imsize ; i++)
  150.     {
  151.                 tmpint = (int) (((float)(*in_short_ptr - min_short)) * scale_io + 0.5);
  152.         *in_short_ptr++ = (unsigned short) tmpint ;
  153.     }
  154.         minmax[0] = 0 ;
  155.         minmax[1] = MAXSHORT ;
  156.     break;
  157.     case PFFLOAT:
  158.     case PFCOMPLEX:
  159.     default:
  160.     fprintf(stderr, "unsupported file format\n");
  161.     exit(-1);
  162.     }
  163.  
  164.     if (imfmt == PFBYTE)
  165.     ahecalc(in_char_image, outimage, dim, minmax, nreg, clip, argv);
  166.     else
  167.     ahecalc_short(in_short_image, outimage, dim, minmax, nreg, clip, argv);
  168.  
  169.     if (frame_index == 0)
  170.         fwrite_header(output, &hipshead,outname);
  171.  
  172.     fwrite((char *)outimage, sizeof(char), imsize, output);
  173.     if (imfmt == PFBYTE)
  174.     free(in_char_image) ;
  175.     else
  176.     free(in_short_image) ;
  177.     fprintf(stderr, "Done with frame %d of %d\n",frame_index+1,num_frames);
  178. }    /* frame_index loop */
  179.     free (outimage) ;
  180. }
  181.  
  182. parse_args(argc, argv)
  183.      int argc;
  184.      char *argv[];
  185. {
  186.     int i;
  187.     int rconflict = 0 ;
  188.  
  189.     for(i = 1; i < argc; i++)
  190.     if(argv[i][0] == '-') switch(argv[i][1]) {
  191.     case 'r':        /* number of regions */
  192.         if (rconflict)
  193.         {
  194.         fprintf (stderr,"conficting options -r and -W\n") ;
  195.         exit(0) ;
  196.         }
  197.         nreg[2] = atoi(argv[++i]);
  198.         nreg[1] = atoi(argv[++i]);
  199.         if ((nreg[2] < 2) || (nreg[1] < 2))
  200.         {
  201.         fprintf (stderr,"number of regions must be at least 2\n") ;
  202.         exit(0) ;
  203.         }
  204.         ++rconflict ;
  205.         break;
  206.     case 'W':        /* region dimensions */
  207.         if (rconflict)
  208.         {
  209.         fprintf (stderr,"conficting options -r and -W\n") ;
  210.         exit(0) ;
  211.         }
  212.         nreg[2] = dim[2]/atoi(argv[++i]);
  213.         nreg[1] = dim[1]/atoi(argv[++i]);
  214.         if ((nreg[2] < 2) || (nreg[1] < 2))
  215.         {
  216.         fprintf (stderr,"region dimensions must be at least 2\n") ;
  217.         exit(0) ;
  218.         }
  219.         ++rconflict ;
  220.         break;
  221.     case 'c':        /* clipping */
  222.         clip = (float) atof(argv[++i]);
  223.         if (clip < 1.0)
  224.         {
  225.         fprintf (stderr,"clip must be greater than 1.0\n") ;
  226.         exit(0) ;
  227.         }
  228. #ifdef DEBUG
  229.         fprintf (stderr, "mahe: clip %d\n", clip);
  230. #endif
  231.         break;
  232.         /* following cases for backwards compatibility with
  233.            Max's original interface */
  234.     case 'd':
  235.     case 'm':
  236.         i += 2 ;
  237.         break ;
  238.     case 'i':
  239.     case 'o':
  240.     case 'w':
  241.         i++ ;
  242.         break ;
  243.     case 'H':
  244.     case 'M':
  245.     case 'f':
  246.     case 'p':
  247.         break ;
  248.     case 'h':
  249.     default:
  250. #ifdef LATER
  251. puts("-m min max    Minima and maxima of data (integer).");
  252. #endif
  253. puts("-r x y        X, Y number of regions (integer), default 4, 4.");
  254. puts("-W x y        X, Y dimensions of a region.");
  255. puts("-c f          Clipping limit f (float), default 0.0.");
  256. puts("-h            Help!");
  257.         exit(0);
  258.     }
  259. }
  260.